package com.wk.springboot.controller;

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Classname FileTransferController
 * @Description TODO
 * @Date 2019/12/5 13:27
 * @Author by weikai
 */
@Controller
@RequestMapping("/transfer")
public class FileTransferController {

  protected Logger logger = LoggerFactory.getLogger(getClass());


  @RequestMapping(value = "/receive", method = RequestMethod.POST)
  public String receive(@RequestParam("files") MultipartFile[] multipartFile, HttpServletRequest request, HttpServletResponse response) {

    try {
      receiveFileByMVC(multipartFile, response);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "/hello";
  }


  /**
   * 接收文件
   */
  private void receiveFileByMVC(MultipartFile[] multipartFile, HttpServletResponse response)
      throws Exception {

    JSONObject jsonObject = new JSONObject();
    boolean success = false;
    String filePath = "S:\\wk\\receive\\";

    //设置request的字符集为UTF-8
    response.setCharacterEncoding("UTF-8");
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    for(MultipartFile file:multipartFile){
      String fileName = file.getOriginalFilename();
      File receiveFile = new File(filePath+fileName);
      System.out.println("要传输到文件"+receiveFile.getName());
      if(!receiveFile.exists()){
        receiveFile.createNewFile();
      }
      file.transferTo(receiveFile);
    }

  }

  /**
   * 接收文件
   */
  private void receiveFile(HttpServletRequest request, HttpServletResponse response)
      throws Exception {

    JSONObject jsonObject = new JSONObject();
    boolean success = false;

    //设置request的字符集为UTF-8
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();

    boolean isUpload = ServletFileUpload.isMultipartContent(request);

    if (isUpload) {
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      try {
        List<FileItem> items = upload.parseRequest(request);
        for (FileItem item : items) {
          String fileName = item.getName();
        }
      } catch (FileUploadException e) {
        e.printStackTrace();
      }
    } else {

    }

  }

}
